/**
 * Ajax工具
 */
Package("shihua.common.Ajax");
shihua.common.Ajax = function(){
	this.state    = "new";//状态有 new,wait,error,success,complete
	this.isSuccess = false;//ajax是否通过正确流程
	this.errorDiv = null;
	this.waitDiv  = null;
	this.type     = "POST";
	this.url      = "";
	this.page     = null;
	this.perPage  = 10;
	this.pageNumber=1;
	this.frugal   = true;//节约型，ajax未完成时，不会再次发送ajax请求
	this.sendOne  = false;//实现ajax只发送一次ajax.send(data)
	this.parameter=null;
	this.async    =true;//异步请求
};
shihua.common.Ajax.prototype = {
	getIsSuccess: function(){
		return this.isSuccess;
	},
	success: function(json){},
	beforeSend: function(){
	},
	complete: function(){},
	error: function(){},
	beforeSuccess: function(json){},
	selfSuccess: function(json){
		this.state = "success";
		this.isSuccess = true;
		if(this.errorDiv && this.errorDiv.length>0){
			this.errorDiv.hide();
		}
		this.json = json;
	},
	afterSuccess: function(json){},
	selfBeforeSend: function(){
		this.state = "wait";
		this.isSuccess = false;
		if (this.waitDiv && this.waitDiv.length>0) {
			this.waitDiv.show();
		}
		if (this.errorDiv && this.errorDiv.length>0) {
			this.errorDiv.hide();
		}
	},
	selfComplete: function(){
		this.state = "complete";
		if (this.waitDiv && this.waitDiv.length>0) {
			this.waitDiv.hide();
		}
	},
	selfError: function(parameter, backFunction){
		this.state = "error";
        this.isSuccess = false;
		if (this.waitDiv) {
			this.waitDiv.hide();
		}
		if(this.errorDiv){
			this.errorDiv.show();
			var self = this;
			$("[key='retry']",this.errorDiv).unbind().click(function(){
				self.send(parameter, backFunction);
				return false;
			});
		}
	},
	send: function(parameter, backFunction){
		if (this.frugal == true) {//节约型，ajax未完成时，不会发送ajax请求
			if (this.state != "complete" && this.state != "new") {//不发送js
				return false;
			}
		}
		if (this.sendOne == true) {//实现当 parameter不改变时，ajax只发送一次ajax.send(data)
			if (this.state == "new" || (this.state == "complete" && !this.isSuccess)) {
				
			} else {
				if ($.toJSON(this.parameter) == $.toJSON(parameter)) {
					return false;
				}
			}
		}
		
		this.parameter = $.evalJSON($.toJSON(parameter));
		if (this.page) {
			this.sendPage(parameter, backFunction);
		} else {
			this.sendSimple(parameter, backFunction);
		}
		
	},
	sendPage: function(parameter, backFunction){
		var self = this;
		var options = {
			parameters: parameter,
			success: function(json){
				self.selfSuccess(json);
				self.beforeSuccess(json);
				self.success(json);
				if (backFunction) {
					backFunction(json)
				}
				if(json&&(json.totalRecord==null||json.totalRecord<=0||json.totalRecord<=self.perPage)){
					self.page.hide();
				}else{
					self.page.show();
				}
				self.afterSuccess(json);
			},
			error: function(){
				self.selfError(parameter, backFunction);
			    self.error();
			},
			startLoad: function(){
				self.selfBeforeSend();
				self.beforeSend();
			},
			overLoad: function(){
				self.selfComplete();
				self.complete();
			},
			type: this.type,
			proxyUrl: this.url,
			perPage: this.perPage,
			pageNumber:this.pageNumber||1
		};
		if(this.state=="new" && this.istatic && this.istatic()){
			this.pagination = shihua.common.Pagination.create(this.page, options,this.totalRecord);
			this.isSuccess = true;
			this.selfComplete();
			return;
		}else{
			this.pagination = shihua.common.Pagination.create(this.page, options);
		}
		this.state = "wait";
		this.isSuccess = false;
	},
	sendSimple: function(parameter, backFunction){
		var self = this;
		var options = {
			data: parameter,
			success: function(json){
				self.selfSuccess(json);
				self.beforeSuccess(json);
				self.success(json);
				if (backFunction) {
					backFunction(json)
				}
				self.afterSuccess(json);
			},
			error: function(){
				self.selfError(parameter, backFunction);
			    self.error();
			},
			beforeSend: function(){
				self.selfBeforeSend();
				self.beforeSend();
			},
			complete: function(){
				self.selfComplete();
				self.complete();
			},
			type: this.type,
			async:this.async
		};
		shihua.common.service.send(this.url, options);
		this.state = "wait";
		this.isSuccess = false;
	}
};
shihua.common.createAjax = function(){
	return new shihua.common.Ajax();
};